from strategies.mvavg import *

def runStrategie(strategieName, code, tradingDays, cost, testDays, window, min):
        df = None

        print("get stategie name:", strategieName)
        if strategieName == "move_average":
            df = getMeanDf(code, window, min)
            percent = 1

        if strategieName == "fixed_investment":
            df = getMeanDf(code, window, min)
            percent = 99999 # 设置为一个很大的数据，让每次都符合条件，变为固定周期定投  

        # 获取最近的N个数据
        df = df.tail(testDays)

        lst = []
        total_cnt = 0
        buy_cnt = 0
        real_buy_cnt = 0
        v  = 0
        value = 0
        result = {}
        
        for x in df.itertuples():
                total_cnt += 1
                if total_cnt % tradingDays == 0:
                    buy_cnt += 1
                    if getattr(x, 'ljjz') / getattr(x, 'ref') < percent:
                        v += cost / getattr(x, "ljjz")
                        real_buy_cnt += 1
                        value = v * getattr(x, 'ljjz')
                lst.append(v)
        print("window:", window)
        result['total_cnt'] = total_cnt
        result['buy_cnt'] = buy_cnt
        result['real_buy_cnt'] = real_buy_cnt
        result['cost'] = real_buy_cnt * cost
        result['value'] = round(value,2)
        if result['cost'] != 0:
            result['income_rate'] = round((value - result['cost']) * 100 / result['cost'], 2)
        else:
            result['income_rate'] = 0

        print(result)

        return result
